/**
 * 用户mixin
 */
import request from '@/common/request.js';
import {BASE_URL} from '@/common/config.js';
import {toast, modal} from '@/common/utils.js';
import {setStorageData} from '@/common/utils.js';
  
module.exports = {
	methods: {
    /**
		 * 登录系统
		 * 
		 * @param {Object} weixinCode
		 * @param {Object} phoneCode
		 * @param {Object} inviterId
		 */ 
		getLogin: function(weixinCode, phoneCode, inviterId) {
      let data = {
        weixin_code: weixinCode,
        phone_code: phoneCode,
        inviter_id: inviterId,
      };
      
      request.httpRequest('weixin_login', data, 'post').then(res => {
				setStorageData('token', res.token);
				setStorageData('userinfo', res.user_info);
				this.userinfo = res.user_info;
      });
    },
    
    /**
     * 上传头像
     * 
     * @param {Object} options
     */
    handleAvatar: function(user){
      uni.uploadFile({
        url: BASE_URL + 'file/avatar',
        filePath: user.avatar,
        name: 'file',
        success(res) {
          let data = JSON.parse(res.data);
          if(200 == data.status) {
            user.avatar = data.data;
          } else {
            modal(data.message);
          }
        }
      });
    }
	}
}
